home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15534 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: liberty.b23a.ingr.com!dpmikese
  2. From: dpmikese@ingr.com (Dave Mikesell)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Dangling pointer?
  5. Date: Fri, 19 Apr 1996 12:56:19
  6. Organization: Intergraph
  7. Message-ID: <dpmikese.15.008B3840@ingr.com>
  8. References: <4l0r4b$jte@dewey.csun.edu> <4l48n6$lj8@texas.nwlink.com>
  9. NNTP-Posting-Host: liberty.b23a.ingr.com
  10. X-Newsreader: Trumpet for Windows [Version 1.0 Rev B final beta #4]
  11.  
  12. In article <4l48n6$lj8@texas.nwlink.com> Teresa Reiko <tjr19@mail.nwlink.com> writes:
  13.  
  14. >kc44097@csun.edu (chen) wrote:
  15. >>
  16. >>    What is "dangling pointer",can someone give me a defination and example?
  17. >>Please e-mail me kc44097@huey.csun.edu
  18.  
  19. >A dangling pointer is, usually, a pointer that pointed to some
  20. >memory allocated with malloc() or calloc() that was then freed.
  21.  
  22. >Dangling pointers cause trouble if the memory they pointed
  23. >to is accessed, since it usually gets re-used.
  24.  
  25. >Example:
  26.  
  27. >char *p;
  28.  
  29. >p = malloc(100);
  30.  
  31. >..
  32.  
  33. >free(p);
  34.  
  35. >/* now p is a dangling pointer */
  36.  
  37. >*p = 0; /* you can't do this */
  38.  
  39.     Actually (and unfortunately) you can do this.  But you shouldn't, 
  40. which I'm sure is what Teresa was trying to say.  But the fact that you can
  41. is the reason it is dangerous - if you assign *p a value you need later, it
  42. may or may not be there when you need it, depending on whether or not its
  43. space on the heap was given to another pointer - after all, it was given back
  44. to the system when free(p) was called.  
  45.  
  46. --
  47. Dave M.
  48.